<?php
/**
 * Created by PhpStorm.
 * User: Shen
 * Date: 2016/6/18
 * Time: 23:21
 */
	// 单文件上传
// 	$fileInfo = $_FILES['myFile'];

require_once 'common.func.php';

/**
 * 构建上传文件信息
 */
function getFiles(){
    $i = 0;
    foreach ($_FILES as $file){
        if (is_string($file['name'])){
            $files[$i] = $file;
            $i++ ;
        }else if (is_array($file['name'])){
            foreach ($file['name'] as $key => $val){
                $files[$i]['name'] = $file['name'][$key];
                $files[$i]['type'] = $file['type'][$key];
                $files[$i]['tmp_name'] = $file['tmp_name'][$key];
                $files[$i]['error'] = $file['error'][$key];
                $files[$i]['size'] = $file['size'][$key];
                $i++ ;
            }
        }
    }
    return $files;
}


function uploadFile($fileInfo,$allowExt=array('jpeg','jpg','png','gif'),$maxSize = 2097152,$flag=true,$uploadPath = './uploads'){

    $res = array();

    // 判断错误号
    if ($fileInfo['error'] === UPLOAD_ERR_OK){
        //检测上传的大小
        if ($fileInfo['size'] > $maxSize){
            $res['msg'] = $fileInfo['name'] . '上传文件过大';
        }

        $ext = getExt($fileInfo['name']);

        //检测上传文件的文件类型
        if (!in_array($ext, $allowExt)){
            $res['msg'] = $fileInfo['name'].'非法文件类型';
        }

        //检测是否是真实的图片类型
        if ($flag){
            if (!getimagesize($fileInfo['tmp_name'])){
                $res['msg'] = $fileInfo['name'] . '不是真实图片类型';
            }
        }

        //检测文件是否是通过POST方式上传的
        if (!is_uploaded_file($fileInfo['tmp_name'])){
            $res['msg'] = $fileInfo['name'] . '文件不是通过HTTP POST方式上传上来的';
        }

        if ($res) return $res;


        if (!file_exists($uploadPath)){
            mkdir($uploadPath,0777,true);
            chmod($uploadPath,0777);
        }

        $uniName = getUniName() . '.' . $ext; //保证唯一性
        $destination = $uploadPath . '/' . $uniName;

        if (!move_uploaded_file($fileInfo['tmp_name'],$destination)){
             $res['msg']  = $fileInfo['name'] . '文件移动失败';
        }


        $res['msg'] = $fileInfo['name'] . '上传成功';

        $res['dest'] =  $destination;




        return $res;

    }else{
        //匹配错误信息
        switch ($fileInfo['error']) {
            case 1:
                $res['msg'] = "大小超过限制";
                break;

            case 2:
                $res['msg'] = "大小超过了html表单中MAX_FILE_SIZE选项指定值";
                break;

            case 3:
                $res['msg'] = "文件只有部分被上传";
                break;

            case 4:
                $res['msg'] = "没有文件被上传";
                break;

            case 6:
                $res['msg'] = "找不到临时文件夹";
                break;

            case 7:
                $res['msg'] = "系统错误，文件写入失败";
                break;

            case 8:
                $res['msg'] = "系统错误，上传文件被PHP扩展文件中断";
                break;

            default:
                break;
        }
        $res['dest'] = '';
        return $res;
    }
}